package org.wu.framework.authorization.platform.infrastructure.persistence;

import org.wu.framework.authorization.platform.infrastructure.entity.SysRolePermissionDO;
import org.wu.framework.authorization.platform.infrastructure.converter.SysRolePermissionConverter;
import org.wu.framework.authorization.platform.domain.model.sys.role.permission.SysRolePermissionRepository;
import org.springframework.stereotype.Repository;

import java.util.stream.Collectors;

import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import jakarta.annotation.Resource;
import org.wu.framework.authorization.platform.domain.model.sys.role.permission.SysRolePermission;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;

import java.util.List;

import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;

/**
 * describe 角色权限关系
 *
 * @author Jia wei Wu
 * @date 2025/08/03 01:55 下午
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class SysRolePermissionRepositoryImpl implements SysRolePermissionRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * describe 新增角色权限关系
     *
     * @param sysRolePermission 新增角色权限关系
     * @return {@link Result<SysRolePermission>} 角色权限关系新增后领域对象
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<SysRolePermission> story(SysRolePermission sysRolePermission) {
        SysRolePermissionDO sysRolePermissionDO = SysRolePermissionConverter.INSTANCE.fromSysRolePermission(sysRolePermission);
        lazyLambdaStream.upsert(sysRolePermissionDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增角色权限关系
     *
     * @param sysRolePermissionList 批量新增角色权限关系
     * @return {@link Result<List<SysRolePermission>>} 角色权限关系新增后领域对象集合
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<List<SysRolePermission>> batchStory(List<SysRolePermission> sysRolePermissionList) {
        List<SysRolePermissionDO> sysRolePermissionDOList = sysRolePermissionList.stream().map(SysRolePermissionConverter.INSTANCE::fromSysRolePermission).collect(Collectors.toList());
        lazyLambdaStream.upsert(sysRolePermissionDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个角色权限关系
     *
     * @param sysRolePermission 查询单个角色权限关系
     * @return {@link Result<SysRolePermission>} 角色权限关系领域对象
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<SysRolePermission> findOne(SysRolePermission sysRolePermission) {
        SysRolePermissionDO sysRolePermissionDO = SysRolePermissionConverter.INSTANCE.fromSysRolePermission(sysRolePermission);
        SysRolePermission sysRolePermissionOne = lazyLambdaStream.selectOne(
                LazyWrappers.<SysRolePermissionDO>lambdaWrapper()
                        .eqIgnoreEmpty(SysRolePermissionDO::getCreateTime, sysRolePermissionDO.getCreateTime())
                        .eqIgnoreEmpty(SysRolePermissionDO::getId, sysRolePermissionDO.getId())
                        .eqIgnoreEmpty(SysRolePermissionDO::getIsDeleted, false)
                        .eqIgnoreEmpty(SysRolePermissionDO::getPermissionId, sysRolePermissionDO.getPermissionId())
                        .eqIgnoreEmpty(SysRolePermissionDO::getRoleId, sysRolePermissionDO.getRoleId())
                        .eqIgnoreEmpty(SysRolePermissionDO::getUpdateTime, sysRolePermissionDO.getUpdateTime())
                        .eqIgnoreEmpty(SysRolePermissionDO::getZoneId, sysRolePermissionDO.getZoneId())
                , SysRolePermission.class);
        return ResultFactory.successOf(sysRolePermissionOne);
    }

    /**
     * describe 查询多个角色权限关系
     *
     * @param sysRolePermission 查询多个角色权限关系
     * @return {@link Result<List<SysRolePermission>>} 角色权限关系领域对象
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<List<SysRolePermission>> findList(SysRolePermission sysRolePermission) {
        SysRolePermissionDO sysRolePermissionDO = SysRolePermissionConverter.INSTANCE.fromSysRolePermission(sysRolePermission);
        List<SysRolePermission> sysRolePermissionList = lazyLambdaStream.selectList(
                LazyWrappers.<SysRolePermissionDO>lambdaWrapper()
                        .eqIgnoreEmpty(SysRolePermissionDO::getCreateTime, sysRolePermissionDO.getCreateTime())
                        .eqIgnoreEmpty(SysRolePermissionDO::getId, sysRolePermissionDO.getId())
                        .eqIgnoreEmpty(SysRolePermissionDO::getIsDeleted, false)
                        .eqIgnoreEmpty(SysRolePermissionDO::getPermissionId, sysRolePermissionDO.getPermissionId())
                        .eqIgnoreEmpty(SysRolePermissionDO::getRoleId, sysRolePermissionDO.getRoleId())
                        .eqIgnoreEmpty(SysRolePermissionDO::getUpdateTime, sysRolePermissionDO.getUpdateTime())
                        .eqIgnoreEmpty(SysRolePermissionDO::getZoneId, sysRolePermissionDO.getZoneId())
                , SysRolePermission.class);
        return ResultFactory.successOf(sysRolePermissionList);
    }

    /**
     * describe 分页查询多个角色权限关系
     *
     * @param size              当前页数
     * @param current           当前页
     * @param sysRolePermission 分页查询多个角色权限关系
     * @return {@link Result<LazyPage<SysRolePermission>>} 分页角色权限关系领域对象
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<LazyPage<SysRolePermission>> findPage(int size, int current, SysRolePermission sysRolePermission) {
        SysRolePermissionDO sysRolePermissionDO = SysRolePermissionConverter.INSTANCE.fromSysRolePermission(sysRolePermission);
        LazyPage<SysRolePermission> lazyPage = new LazyPage<>(current, size);
        LazyPage<SysRolePermission> sysRolePermissionLazyPage = lazyLambdaStream.selectPage(
                LazyWrappers.<SysRolePermissionDO>lambdaWrapper()
                        .eqIgnoreEmpty(SysRolePermissionDO::getCreateTime, sysRolePermissionDO.getCreateTime())
                        .eqIgnoreEmpty(SysRolePermissionDO::getId, sysRolePermissionDO.getId())
                        .eqIgnoreEmpty(SysRolePermissionDO::getIsDeleted, false)
                        .eqIgnoreEmpty(SysRolePermissionDO::getPermissionId, sysRolePermissionDO.getPermissionId())
                        .eqIgnoreEmpty(SysRolePermissionDO::getRoleId, sysRolePermissionDO.getRoleId())
                        .eqIgnoreEmpty(SysRolePermissionDO::getUpdateTime, sysRolePermissionDO.getUpdateTime())
                        .eqIgnoreEmpty(SysRolePermissionDO::getZoneId, sysRolePermissionDO.getZoneId())
                , lazyPage, SysRolePermission.class);
        return ResultFactory.successOf(sysRolePermissionLazyPage);
    }

    /**
     * describe 删除角色权限关系
     *
     * @param sysRolePermission 删除角色权限关系
     * @return {@link Result<SysRolePermission>} 角色权限关系
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<SysRolePermission> remove(SysRolePermission sysRolePermission) {
        SysRolePermissionDO sysRolePermissionDO = SysRolePermissionConverter.INSTANCE.fromSysRolePermission(sysRolePermission);
        lazyLambdaStream.delete(
                LazyWrappers.<SysRolePermissionDO>lambdaWrapper()
                        .eqIgnoreEmpty(SysRolePermissionDO::getCreateTime, sysRolePermissionDO.getCreateTime())
                        .eqIgnoreEmpty(SysRolePermissionDO::getId, sysRolePermissionDO.getId())
                        .eqIgnoreEmpty(SysRolePermissionDO::getIsDeleted, false)
                        .eqIgnoreEmpty(SysRolePermissionDO::getPermissionId, sysRolePermissionDO.getPermissionId())
                        .eqIgnoreEmpty(SysRolePermissionDO::getRoleId, sysRolePermissionDO.getRoleId())
                        .eqIgnoreEmpty(SysRolePermissionDO::getUpdateTime, sysRolePermissionDO.getUpdateTime())
                        .eqIgnoreEmpty(SysRolePermissionDO::getZoneId, sysRolePermissionDO.getZoneId())
        );
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在角色权限关系
     *
     * @param sysRolePermission 角色权限关系领域对象
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<Boolean> exists(SysRolePermission sysRolePermission) {
        SysRolePermissionDO sysRolePermissionDO = SysRolePermissionConverter.INSTANCE.fromSysRolePermission(sysRolePermission);
        Boolean exists = lazyLambdaStream.exists(
                LazyWrappers.<SysRolePermissionDO>lambdaWrapper()
                        .eqIgnoreEmpty(SysRolePermissionDO::getCreateTime, sysRolePermissionDO.getCreateTime())
                        .eqIgnoreEmpty(SysRolePermissionDO::getId, sysRolePermissionDO.getId())
                        .eqIgnoreEmpty(SysRolePermissionDO::getIsDeleted, false)
                        .eqIgnoreEmpty(SysRolePermissionDO::getPermissionId, sysRolePermissionDO.getPermissionId())
                        .eqIgnoreEmpty(SysRolePermissionDO::getRoleId, sysRolePermissionDO.getRoleId())
                        .eqIgnoreEmpty(SysRolePermissionDO::getUpdateTime, sysRolePermissionDO.getUpdateTime())
                        .eqIgnoreEmpty(SysRolePermissionDO::getZoneId, sysRolePermissionDO.getZoneId())
        );
        return ResultFactory.successOf(exists);
    }

    /**
     * 删除角色权限
     *
     * @param roleId
     */
    @Override
    public void removeRolePermission(Long roleId) {
        SysRolePermissionDO sysRolePermissionDO = new SysRolePermissionDO();
        sysRolePermissionDO.setIsDeleted(true);
        lazyLambdaStream.update(sysRolePermissionDO, LazyWrappers.<SysRolePermissionDO>lambdaWrapper()
                .eq(SysRolePermissionDO::getRoleId, roleId)
        );
    }
}